package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Auth;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.AuthService;
import com.example.demo.mapper.AuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* @author 三石
* @description 针对表【auth(系统菜单)】的数据库操作Service实现
* @createDate 2024-05-06 21:39:48
*/
@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, Auth>
    implements AuthService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<Auth> getMenu(String username) {

        List<Long> roleIds = userMapper.selectRoleIdsByUsername(username);
        System.out.println("roleIds"+roleIds);
        List<Long> authIds = roleMapper.selectAuthorIdsByIds(roleIds);
        System.out.println("authIds"+authIds);

//        List<Auth> auths = baseMapper.selectMenuByAuthIds(authIds);
//        System.out.println("auths"+auths);
//        //寻找一级菜单
//        LinkedList<Auth> menus = new LinkedList<>();
//        auths.forEach(auth -> {
//            if (auth.getPid()==0){
//                menus.add(auth);
//            }
//        });
//
//        //寻找二级菜单
//        LinkedList<Auth> children = new LinkedList<>();
//        menus.forEach(auth -> {
//            auths.forEach(auth1 -> {
//                if (auth1.getPid().equals(auth.getId())){
//                    children.add(auth1);
//                }
//            });
//            auth.setChildren(children);
//        });
//        return menus;
        List<Auth> auths = baseMapper.selectMenuByAuthIds(authIds);
        System.out.println("auths: " + auths);

        List<Auth> secondMenus = new ArrayList<>();
        auths.forEach(auth -> {
            if (auth.getPid() != 0) {
                secondMenus.add(auth);
            }
        });

        System.out.println(secondMenus);
        return secondMenus;
    }

    @Override
    public List<Auth> getTopMenu(String username) {
        List<Long> roleIds = userMapper.selectRoleIdsByUsername(username);
        System.out.println("roleIds"+roleIds);
        List<Long> authIds = roleMapper.selectAuthorIdsByIds(roleIds);
        System.out.println("authIds"+authIds);

        List<Auth> auths = baseMapper.selectTopMenuByAuthIds(authIds);
        //寻找顶部菜单
        LinkedList<Auth> menus = new LinkedList<>();
        auths.forEach(auth -> {
            if ("0".equals(auth.getType())){
                menus.add(auth);
            }
        });

        //寻找一级菜单
        LinkedList<Auth> children = new LinkedList<>();
        menus.forEach(auth -> {
            auths.forEach(auth1 -> {
                if (auth1.getPid().equals(auth.getId())){
                    children.add(auth1);
                    auth.setChildren(children);
                }
            });

        });
        System.out.println(menus);
        return menus;
    }
}


//    public List<Auth> getMenu(String username) {
//
//        List<Long> roleIds = userMapper.selectRoleIdsByUsername(username);
//        System.out.println("roleIds" + roleIds);
//        List<Long> authIds = roleMapper.selectAuthorIdsByIds(roleIds);
//        System.out.println("authIds" + authIds);
//
//        List<Auth> auths = baseMapper.selectMenuByAuthIds(authIds);
//        System.out.println("auths" + auths);
//
//        // 寻找二级菜单
//        LinkedList<Auth> children = new LinkedList<>();
//        auths.forEach(auth -> {
//            if (auth.getPid() != 0) {
//                children.add(auth);
//            }
//        });
//
//        return children;
//    }
//}

